/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
package com.pingan.mobilegw.biz.shared.router;

import com.pingan.mobilegw.adapterservice.ResultEnum;
import com.pingan.mobilegw.biz.shared.router.post.PostProcessor;
import com.pingan.mobilegw.core.model.GatewayRequest;
import com.pingan.mobilegw.core.model.GatewayResponse;
import com.pingan.mobilegw.core.model.enums.RequestSceneEnum;
import com.pingan.mobilegw.core.model.exception.MgwException;
import com.pingan.mobilegw.core.model.request.SingleRequestBody;
import com.pingan.mobilegw.core.service.business.BusinessComponent;
import com.pingan.mobilegw.core.service.business.Processor;
import com.pingan.mobilegw.core.service.util.ResponseUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务路由器
 * 
 * @author jiadao
 * @version $Id: ServiceRouterImpl.java, v 0.1 2013-4-4 下午8:12:20 jiadao Exp $
 */
@Service("serviceRouter")
public class ServiceRouterImpl implements ServiceRouter {

    /** logger */
    private static final Log logger = LogFactory.getLog(ServiceRouterImpl.class);

    /** 业务组件 */
    @Autowired
    private BusinessComponent   businessComponent;

    /** 安全校验器 */
    private SecurityManager     securityManager;

    /** 后置处理器 */
    private List<PostProcessor> postProcessors;

    @Override
    public GatewayResponse processRoute(GatewayRequest request) {
        logger.debug("服务路由请求："+request);

        try {
            //======================前置检查和操作======================
            //TODO securityManager.check(request, RequestSceneEnum.SINGLE);

            //======================业务执行======================
            SingleRequestBody requestBody = (SingleRequestBody) request.getRequestBody();
            Processor processer = businessComponent.getProcessor(requestBody.getOperationType());
            GatewayResponse response = processer.process(request);

            //======================后置业务执行======================
            //if (response != null)
              //  afterProcess(request, response);

            return response;
        } catch (MgwException e) {
            logger.warn("无线网关路由错误," + e);
            return ResponseUtil.buildErrorResponse(e);
        } catch (Exception e) {
            logger.error("服务路由未知异常", e);
            return ResponseUtil.buildErrorResponse(ResultEnum.UnknowError);
        }

    }

    /**
     * 后置业务处理
     * @param response 
     * @param request 
     */
    private void afterProcess(GatewayRequest request, GatewayResponse response) {

        for (PostProcessor postProcessor : postProcessors) {
            postProcessor.execute(request, response);
        }
    }

    /**
     * Setter method for property <tt>businessComponent</tt>.
     * 
     * @param businessComponent value to be assigned to property businessComponent
     */
    public void setBusinessComponent(BusinessComponent businessComponent) {
        this.businessComponent = businessComponent;
    }

    /**
     * Setter method for property <tt>securityManager</tt>.
     * 
     * @param securityManager value to be assigned to property securityManager
     */
    public void setSecurityManager(SecurityManager securityManager) {
        this.securityManager = securityManager;
    }

    /**
     * Setter method for property <tt>postProcessors</tt>.
     * 
     * @param postProcessors value to be assigned to property postProcessors
     */
    public void setPostProcessors(List<PostProcessor> postProcessors) {
        this.postProcessors = postProcessors;
    }

}
